package implementation;

import protocol.Query;
import protocol.State;
import protocol.StateObject;

public class HomeTeamHandler {

	public static void handleHomeTeam(StateObject nextState, Query query) {
		String numTickets = query.getNumTix();
		String standardPrompt = "HomeTeamQuestion%" + numTickets;
		String helpPrompt = "HomeTeamHelp";
		ExecUtils.playPrompt(standardPrompt, nextState);

		boolean receivedInput = false;
		while (!receivedInput) {
			ExecUtils.getUserInput(query, "gram_hometeam");
			//user provided a directive
			if (query.getShortConf() != null) {
				String response = query.getShortConf().trim().toLowerCase();
				if (response.equals("assistance"))
					ExecUtils.playPrompt(helpPrompt, nextState);
				else if (response.equals("goback") || response.equals("go back") || 
						response.equals("startover") || response.equals("start over")) {
					ExecUtils.playPrompt(response, nextState);
					receivedInput = true;
					nextState.setState(State.NUMTIX);
				}
				query.voidTransaction();
			} 
			//user did not give the desired input, but may have provided other
			else if (query.getHomeTeam() == null) {
				//complex query
				if (query.awayTeamIsNew() || query.dateIsNew() || query.priceIsNew()) {
					boolean success = ExecUtils.explicitConfirm(query, nextState);
					if (!success)
						query.voidTransaction();
					ExecUtils.playPrompt(standardPrompt, nextState);
				}
				//no input
				else {
					query.voidTransaction();
					ExecUtils.playPrompt(helpPrompt, nextState);
				}
			}
			//desired input was received
			else {
				receivedInput = true;

				//complex query
				if (query.awayTeamIsNew() || query.dateIsNew() || query.priceIsNew())
				{
					boolean success = ExecUtils.explicitConfirm(query, nextState);
					if (!success)
					{
						query.voidTransaction();
						receivedInput = false;
						ExecUtils.playPrompt(standardPrompt, nextState);
					}
				}

				//choose next state
				if(receivedInput){
					if (query.getAwayTeam() == null)
					{
						nextState.setState(State.AWAYTEAM);
						System.out.println("set next state to away team");
					}
					else
						nextState.setState(State.AWAYTEAMCONF);
				}
			}
		}
	}
}
